<?php

namespace App\Http\Controllers\Admin\Demo;

use App\Http\Controllers\AdminBaseController;
use Illuminate\Http\Request;
use App\Libs\Commons\MyMessage;
use App\Libs\Commons\MyLogger;
use App\Models\DemoModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class IndexController extends AdminBaseController
{
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * @desc		完整版手动注释
	 * 
	 * @OA\Get(path="/admin/Demo/Index/index",
	 * 	tags={"Demo"},
	 * 	summary="index示例方法",
	 * 	@OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string", default="123456")),
	 * 	@OA\Parameter(
	 * 		name="id",
	 * 		in="query",
	 * 		description="输入用户ID",
	 * 		@OA\Schema(type="int",default="1")
	 * 	),
	 * 	@OA\Response(
	 * 		response="200",
	 * 		description="成功响应",
	 * 		@OA\MediaType(
	 * 			mediaType="application/json",
	 * 			@OA\Schema(
	 * 				example={"code": "1", "data": {"author":"haochezhu"}, "message": "success"},
	 * 				@OA\Property(
	 *         			property="code",
	 *         			type="string",
	 *         			description="状态码"
	 *         		),
	 *         		@OA\Property(
	 *         			property="data",
	 *         			type="array",
	 *                 	@OA\Items(
	 *                 		@OA\Property(
	 *                 			property="author",
	 *                         	type="string",
	 *                         	description="作者"
	 *                     	)
	 *                 	)
	 *             	),
	 *             	@OA\Property(
	 *                 property="message",
	 *                 type="string",
	 *                 description="message消息"
	 *             )
	 *         )
	 *     )
     *    ),
     *    @OA\Response(
	 * 		response="400",
	 * 		description="失败响应",
	 * 		@OA\JsonContent(ref="#/components/schemas/BaseJsonContent")
	 * 	)
     *  )
	 */
	public function index(Request $req){
		//日志记录
		MyLogger::debug($message='start', ['all' => $req->all()]);
		MyLogger::log(MyLogger::ERROR, $message='end', ['all' => $req->all()]);
		
		if(!empty($req->get('model'))){
		    //获取一条信息 返回对象
		    $first = DemoModel::where('id', '!=', 0)->first();
		    print_r($first);
		    echo "<br>";
		    
		    //手动指定DB, 返回数组
		    $result = DB::connection('myCustomDB')->table(DemoModel::getTableName())
		    ->where('id', '!=', 0)->limit(5)->get()->toArray();
		    print_r($result);
		}

		if(!empty($req->get('redis'))){
			echo Redis::get('author') . "<br>";
		}
		
		$mark = $req->get('mark') ?? 'success';
		
		if($mark=='error'){
			return My_error(MyMessage::PARAM_EMPTY, '选填的自定义错误信息');
		}
		return My_success(['author' => 'haochezhu']);
	}
}
